﻿// Author: Liep Nguyen, 11:10 PM 3/10/2013
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace RoutingAlgorithms
{
    class Topology
    {
        List<Node> nodes;

        public List<Node> Nodes
        {
            get { return this.nodes; }
        }

        // Find a node
        public Node GetNode(int id)
        {
            foreach (Node n in this.nodes)
            {
                if (n.Id == id) return n;
            }
            return new Node();
        }

        // Add a node to map
        public void AddNode(Node node)
        {
            try
            {
                this.nodes.Add(node);
            }
            catch
            {
                Console.WriteLine("Can't add node to map!!!");
            }
        }

        // Clear map
        public void Clear()
        {
            try
            {
                this.nodes.Clear();
            }
            catch
            {
                Console.WriteLine("Can't clear this map");
            }
        }


        // Remove a link 
        public void RemoveLink(Node source, Node destination)
        {
            foreach (Link link in source.Links)
            {
                if (link.Destination == destination)
                {
                    source.Links.Remove(link);
                    break;
                }
            }
        }

        // Remove all link
        private void RemoveAllLink()
        {
            if (this.nodes.Count > 1) 
            {
                foreach (Node n in this.nodes)
                {
                    n.Links.Clear();
                }
            }
        }

        // CONTRUCTOR
        // Create new map

        public Topology()
        {
            nodes = new List<Node>();
        }

        public void load(String filePath)
        {
            FileStream file = new FileStream(filePath, FileMode.Open);
            StreamReader reader = new StreamReader(file);

            int n = int.Parse(reader.ReadLine());
            for (int i = 0; i < n; i++)
                nodes.Add(new Node(i));

            while (!reader.EndOfStream)
            {
                string[] value = reader.ReadLine().Split('\t');
                Node nodeA = nodes[int.Parse(value[0])];
                Node nodeB = nodes[int.Parse(value[1])];
                string mode = value[2];
                double capacity = double.Parse(value[3]);

                switch (mode)
                {
                    case "single":
                        {
                            Link link = new Link(nodeA, nodeB, capacity);
                            nodeA.addLink(link);
                            break;
                        }
                    case "double":
                        {
                            Link link = new Link(nodeA, nodeB, capacity);
                            Link rLink = new Link(nodeB, nodeA, capacity);
                            nodeA.addLink(link);
                            nodeB.addLink(rLink);
                            break;
                        }
                }
            }
        }
    }
}

